$().ready(function(){
    $("#simple .position").sortable({
        connectWith: '.position, .block',
        containment: 'body',
        items: '.widget',
        start:function(event, ui){
            if(ui.item.hasClass('block')){
                ui.item.sortable("destroy");
            }
        },
        stop:function(){
            
        }
    });
    
    sym_page_simple_load_all();
    $('#simple-buttons input, #simple-buttons a').button();
    $('#simple-buttons-save').click(function(){
        sym_page_simple_save();
        sym_page_simple_load_all();
        return false;
    });
    $('#simple-buttons-save-empty').click(function(){
        sym_page_simple_save('recycle_bin');
        sym_page_simple_load_all();
        return false;
    });
    $('#simple-buttons-cencel').click(function(){
        sym_page_simple_load_all();
        return false;
    });
    $('#simple-buttons-add').click(function(){
        $(this).blur();
        sym_page_simple_add();
        return false;
    });
});
function sym_page_simple_widget_add(to, name, symbiont, symbiont_new, access, block){
    if(to==undefined) return;
    if(name==undefined) return;
    if(symbiont==undefined) symbiont='';
    if(symbiont_new==undefined) symbiont_new='';
    if(access==undefined) access=0;
    if(block==undefined) block=false;
    if(name==='') name=symbiont;
    to.append("<div class=\"widget\" symbiont=\""+symbiont+"\" symbiont_new=\""+symbiont_new+"\" access=\""+access+"\"><label>"+name+"</label></div>");
    to.find('.widget:last').dblclick(function(){
        sym_page_simple_edit($(this));
    })
    if(block){
        to.find('.widget:last')
        .append("<div class=\"block\" title=\""+symbiont+"\"></div>")
        .find('.block')
        .sortable({
            connectWith: '.position, .block',
            containment: 'body',
            items: '.widget'
        });
        sym_page_simple_load(to.find('.widget:last .block'), symbiont);
    }
}
function sym_page_simple_save(empty){
    json={};
    $(".position, .block").each(function(){
        title=$(this).attr('title');
        json[title]=[];
        widget=$(this).children('.widget');
        widget.each(function(){
            symbiont=$(this).attr('symbiont');
            symbiont_new=$(this).attr('symbiont_new');
            access=$(this).attr('access');
            t=$(this).text();
            if(t==symbiont||t==symbiont_new) t='';
            if(empty==title){
                e=true;
                $(this).remove();
            }
            else e=false;
            json[title].push({'symbiont':symbiont, 'symbiont_new':symbiont_new, 'access':access, 'title':t, 'empty':e});
        });
        if(!widget.length){
            json[title].push('');
        }
    });
    $.ajax({
        'data': {
            'symbiont': 'page.db_save',
            'json': json
        },
        'success':function(r){
            
        }
    });
    
    
}
function sym_page_simple_load_all(){
    $('.position').each(function(){
        title=$(this).attr('title');
        sym_page_simple_load($(this), title);
    });
}
function sym_page_simple_load(to, title){
    to.find('.widget').remove();
    if(title){
        $.ajax({
            data:{
                symbiont: 'page.json_symbionts',
                position: title
            },
            success:function(r){
                for(key in r.symbiont){
                    sym_page_simple_widget_add(to, r.title[key], r.symbiont[key], '', r.access[key], r.block[key]);
                }
            }
        });
    }
}
function sym_page_simple_add(){
    if($('#sym_page_simple_add').length){
        $('#sym_page_simple_add').dialog("open");
    }
    else{
        $('#windows').append('<div id="sym_page_simple_add"><label class="symbiosis-label">Symbiont: </label><select><option></option></select><div class="content"></div></div>');
        $('#sym_page_simple_add').dialog({
            modal: true,
            position: ['center', 100],
            open: function(event, ui) {
                $('#sym_page_simple_add .content').html('');
                $.ajax({
                    data:{
                        symbiont: 'page.json_symbionts_all'
                    },
                    success:function(r){
                        $('#sym_page_simple_add select').html('<option></option><option value="page">For developers</option>');
                        for(key in r){
                            $('#sym_page_simple_add select').append('<option value="'+r[key].symbiont+'">'+r[key].title+'</option>');
                        }
                    }
                });
                $('#sym_page_simple_add select').change(function(){
                    symbiont=$(this).val();
                    if(symbiont){
                        $.ajax({
                            data:{
                                administrate: symbiont
                            },
                            success:function(r){
                                $('#sym_page_simple_add .content').html(r).sym();
                            },
                            dataType: 'html'
                        });
                    }
                });
            },
            buttons:{
                "Cencel":function(){
                    $(this).dialog("close"); 
                },
                "Ok":function(){
                    $(this).dialog("close");
                    $(this).find('form').submit();
                    title='';
                    symbiont='';
                    $(this).find('input').each(function(){
                        if($(this).attr('name')=='title'){
                            title=$(this).val();
                        }
                        if($(this).attr('name')=='symbiont'){
                            symbiont_new=$(this).val();
                        }
                    });
                    if(!title){
                        title=symbiont_new;
                    }
                    if(symbiont_new){
                        sym_page_simple_widget_add($('.position[title=recycle_bin]'), title, '', symbiont_new);
                    }
                }
            }
        });
    }
}
function sym_page_simple_edit(element){
    symbiont=element.attr('symbiont');
    if(!symbiont) symbiont=element.attr('symbiont_new');
    if($('#sym_page_simple_edit').length){
        $('#sym_page_simple_edit').dialog("open");
    }
    else{
        $('#windows').append('<div id="sym_page_simple_edit"></div>');
        $('#sym_page_simple_edit').dialog({
            modal: true,
            width: 500,
            position: ['center', 100],
            open: function(event, ui) {
                $.ajax({
                    data:{
                        'administrate': symbiont
                    },
                    success:function(r){
                        $('#sym_page_simple_edit').html(r).sym();
                    },
                    dataType: 'html'
                });
            },
            buttons:{
                "Cencel":function(){
                    $('#sym_page_simple_edit').html('');
                    $(this).dialog("close"); 
                },
                "Ok":function(){
                    var symbiont_new='';
                    var access='';
                    var name='';
                    symbiont_new=$('#sym_page_simple_edit input[name=symbiont]').val();
                    access=$('#sym_page_simple_edit input[name=access]').val();
                    title=$('#sym_page_simple_edit input[name=title]').val();
                    if(title==''||title==undefined) title=symbiont_new;
                    if(symbiont_new==undefined) symbiont_new='';
                    if(access==undefined) access=0;
                    element
                    .attr('symbiont_new', symbiont_new)
                    .attr('access', access)
                    .children('label')
                    .html(title);
                    $('#sym_page_simple_edit form').submit();
                    $('#sym_page_simple_edit').html('');
                    $(this).dialog("close");
                }
            }
        });
    }
}